package com.dscs;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Huatu {
    private static final int width = 375;
    private static final int height = 812;
    private static final int fontSize = 16;
    private static final int lineSize = 21;//每行字数
    private static final int ww = 20;//行头
    private static final int line = 33;//行头
    private static String watermark = "xxxx";//水印文本

    public static void main(String[] args) {
        File file = new File("C:\\Users\\13522\\Desktop\\xs\\卷一-第一章 青云.txt");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
            BufferedReader novelbr = new BufferedReader(inputStreamReader);
            int currentIndex = 1;
            String line = novelbr.readLine();
            List<String> lines = new ArrayList<>();
            while (line != null) {
                lines.add(line);
                line = novelbr.readLine();
            }
            novelbr.close();
            fileInputStream.close();
            int tag = 1;
            drbm(file.getName(), tag, lines);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void drbm(String fileName, int page, List<String> lines) {
        // 创建BufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取Graphics2D
        Graphics2D g2d = image.createGraphics();
        // 画图
        g2d.setBackground(new Color(255, 255, 255));
        //g2d.setPaint(new Color(0,0,0));
        g2d.setColor(Color.red);
        g2d.clearRect(0, 0, width, height);
        Font font = loadFont("C:\\Users\\13522\\Desktop\\SourceHanSansCN-Regular.ttf", fontSize);
        g2d.setFont(font);
        // 抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 计算文字长度，计算居中的x点坐标
        FontMetrics fm = g2d.getFontMetrics(font);

        int tag = 0;
        int pageLine = 1;
        String moreThan = "";
        for (String text : lines) {
            List<String> linesss = new ArrayList<>();
            handle(linesss, text);
            int textWidth = fm.stringWidth(text);
            int ascent = fm.getAscent();
            int textHeight = fm.getHeight();
            int widthX = (width - textWidth) / 2;
            if ((tag + linesss.size()) > line) { //总行数超出图片范围
                // 释放对象
                g2d.dispose();
                // 保存文件
                try {
                    File outFile = new File("C:\\Users\\13522\\Desktop\\xs\\" + fileName.replaceAll(".txt", "") + "-" + page + ".png");
                    watermarkText(image, outFile);
//                    ImageIO.write(image, "png", outFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //继续绘制剩下的  pageLine - 1 当前行还没有绘制所以 -1 page+1 页码加1
                drbm(fileName, page += 1, lines.subList(pageLine - 1, lines.size()));
                return;
            }
            for (String s : linesss) {
                // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
                g2d.drawString(s, ww, textHeight * tag + ascent + 10);
                tag++;
            }
            pageLine++;
        }
        // 释放对象
        g2d.dispose();
        // 保存文件
        try {
            File outFile = new File("C:\\Users\\13522\\Desktop\\xs\\" + fileName.replaceAll(".txt", "") + "-" + page + ".png");
            watermarkText(image, outFile);
//            ImageIO.write(image, "png", outFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void watermarkText(BufferedImage bi, File file) throws IOException {
        Thumbnails.of(bi)
                .size(bi.getWidth(), bi.getHeight())
                .watermark(Positions.CENTER, ImageIO.read(new File("path/to/watermark.png")), 0.5f)
                .outputQuality(0.8)
                .toFile(file);

//        Graphics2D graphics = bi.createGraphics();
//        // 设置“抗锯齿”的属性
//        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        // 设置字体类型和大小
//        Font font = loadFont("C:\\Users\\13522\\Desktop\\SourceHanSansCN-Regular.ttf", 22);
//        graphics.setFont(font);
//        // 设置颜色
//        graphics.setColor(new Color(255,0,0,80));
//        // 添加文字
//        graphics.drawString(watermark, 10, 10);
//        graphics.dispose();
    }

    private static void handle(List<String> liness, String line) {
        if (line.length() > lineSize) {
            liness.add(line.substring(0, lineSize));
            handle(liness, line.substring(lineSize));
        } else {
            liness.add(line);
        }
    }

    public static Font loadFont(String fontFileName, float fontSize)  //第一个参数是外部字体名，第二个是字体大小
    {
        try {
            File file = new File(fontFileName);
            FileInputStream aixing = new FileInputStream(file);
            Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, aixing);
            Font dynamicFontPt = dynamicFont.deriveFont(fontSize);
            aixing.close();
            return dynamicFontPt;
        } catch (Exception e)//异常处理
        {
            e.printStackTrace();
            return new java.awt.Font("宋体", Font.PLAIN, 12);
        }
    }
}
